#include "activity.h"
#include "participant.h"
#include <QDebug>
#include <QTimer>

Activity::Activity(QObject *parent) :
    QObject(parent),
    name(""),
    startTime(QDateTime::currentDateTime()),
    stopTime(QDateTime::currentDateTime()),
    regTimeout(0),
    deltaTime(0),
    currentTime(0),
    regTerminated(false),
    activityLoaded(false)
{
    timer = new QTimer(this);
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(registrationTimeout()));
}

Activity::Activity(QObject *parent, Participant *activityOrganiser, QByteArray activityUid, QString activityName, QDateTime activityStartTime, QDateTime activityStopTime, int activityRegTimeout, bool loaded) :
    QObject(parent),
    name(""),
    startTime(QDateTime::currentDateTime()),
    stopTime(QDateTime::currentDateTime()),
    regTimeout(0),
    deltaTime(0),
    currentTime(0),
    regTerminated(false),
    activityLoaded(false)
{
    timer = new QTimer(this);
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(registrationTimeout()));
    organiser = new Participant(this, activityOrganiser->getTagId(), activityOrganiser->getTagType(), activityOrganiser->getName(), activityOrganiser->getStatus(), activityOrganiser->getScanTime());
    name = activityName;
    startTime = activityStartTime;
    stopTime = activityStopTime;
    regTimeout = activityRegTimeout;
    activityLoaded = loaded;
    uid = activityUid;
}

QString Activity::getName(void)
{
    qDebug()<<"Request for name NAME="<<name;
    return name;
}

QDateTime Activity::getStartTime(void)
{
    return startTime;
}

QDateTime Activity::getStopTime(void)
{
    return stopTime;
}

int Activity::getRegTimeout()
{
    return regTimeout;
}

QByteArray Activity::getUid(void)
{
    return uid;
}

Participant* Activity::getOrganiser(void)
{
    return organiser;
}

void Activity::registrationTimeout()
{
    if(currentTime + deltaTime < regTimeout)
        currentTime += deltaTime;
    else if(currentTime + deltaTime > regTimeout)   {
        deltaTime = regTimeout - currentTime;
        timer->start(deltaTime);
    }
    else {
        timer->stop();
        currentTime += deltaTime;
        regTerminated = true;

        emit update(RegistrationTimeout);
    }
    emit timeoutProgress(currentTime);
}

void Activity::dataUpdated(Participant *activityOrganiser, QByteArray activityUid, QString activityName, QDateTime activityStartTime, QDateTime activityStopTime, int activityRegTimeout)
{
    if(!activityLoaded) {
        organiser = new Participant(this, activityOrganiser->getTagId(), activityOrganiser->getTagType(), activityOrganiser->getName(), activityOrganiser->getStatus(), activityOrganiser->getScanTime());
        name = activityName;
        startTime = activityStartTime;
        stopTime = activityStopTime;
        regTimeout = activityRegTimeout;
        uid = activityUid;
    }
}

void Activity::activityUpdated(Activity::ActivityStatus status)
{
        if(regTimeout > 0)  {
            deltaTime = regTimeout/30;
            currentTime = 0;
            timer->start(deltaTime);
        }
        emit update(status);
}

void Activity::participantAdd(Participant *p)
{
    if(p != NULL && !activityLoaded)  {
        Participant *n = new Participant(this, p->getTagId(), p->getTagType(), p->getName(), p->getStatus(), p->getScanTime());
        participantList.append(n);
        qDebug() << "Participant added to list";
        emit update(ParticipantAdded);
    }
    else
        activityLoaded = true;
}

void Activity::participantUpdated(Participant *p)
{
    if(regTerminated)
        emit update(RegistrationTimeout);
    else if(activityLoaded)
    {
        if(p != NULL)   {
            int i = 0;
            for(i = 0; i < participantList.count(); i++)
                if(participantList[i]->getTagId() == p->getTagId())
                    break;
            if(i < participantList.count() && (participantList[i]->getStatus() == Participant::ParticipantUnknown || participantList[i]->getStatus() == Participant::ParticipantPresent))   {
                participantList[i]->checkIn();
                qDebug() << "Participant checked in";
                emit update(ParticipantUpdated);
            }
            else    {
                qDebug() << "Participant Unknown";
                emit update(ParticipantUnknown);
            }
        }
        else    {
            qDebug() << "Participant Unknown";
            emit update(ParticipantUnknown);
        }
    }
    else    {
        emit update(ParticipantUnknown);
    }
}

void Activity::organiserSignal(Participant *p, QByteArray approvedId)
{
    if(regTerminated)
        emit update(RegistrationTimeout);
    else if(approvedId > 0 && p->getTagType() == Participant::Organiser)   {
        int i = 0;
        for(i = 0; i < participantList.count(); i++)
            if(participantList[i]->getTagId() == approvedId)    {
                participantList[i]->checkIn();
                emit update(ParticipantManuallyCheckedIn);
                break;
            }
        if(i == participantList.count()) {
            qDebug() << "ERROR - approvedId not found";
            emit update(ParticipantNotFound);
        }
    }
    else if(p->getTagType() == Participant::Organiser) {

        emit update(OrganiserScanned);
    }
    else
        emit update(IllegalApprover);
}

void Activity::setActivityLoaded()
{
    activityLoaded = true;
}

bool Activity::isLoaded()
{
    return activityLoaded;
}
